import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import AutoImport from 'unplugin-auto-import/vite'
import path from 'path'

const pathSrc = path.resolve(__dirname, 'src')

export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd())
    const { VITE_APP_BASE_URL, VITE_APP_BASE_PRE } = env

    return {
        resolve: {
            alias: {
                '@': path.resolve(__dirname, './src')
            }
        },
        plugins: [
            uni(),
            AutoImport({
                imports: [
                    'vue',
                    'uni-app',
                    '@vueuse/core',
                    {
                        '@/utils': [
                            'debounce',
                            'throttle',
                            'validatePhone',
                            'validateEmail',
                            'validatePassword',
                            'formatDate',
                            'formatFileSize',
                            'formatNumber',
                            'generateUUID',
                            'getImageUrl',
                            'getHeaderImage',
                            'deepClone',
                            'downloadFile',
                            'exportCsv',
                            'exportJson'
                        ],
                        '@/hooks': [
                            'useStorage',
                            'usePerformance',
                            'useCache'
                        ]
                    }
                ],
                vueTemplate: true,
                dts: path.resolve(pathSrc, 'types', 'auto-imports.d.ts')
            })
        ],
        server: {
            host: '0.0.0.0',
            port: 3000,
            open: false,
            proxy: {
                [VITE_APP_BASE_PRE]: {
                    target: VITE_APP_BASE_URL,
                    ws: true,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(new RegExp('^' + VITE_APP_BASE_PRE), '')
                }
            }
        }
    }
})
